﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;

namespace FilStartUps
{
    public partial class MembersList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["Skip"] = 0;
                GetMemberList();
            }
        }

        private void GetMemberList()
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var members = (from m in db_connect.Members
                               join p in db_connect.Profiles
                                  on m.memberidPK equals p.memberidFK
                               orderby p.lastname
                               select new MemberList
                               {
                                   memberidPK = m.memberidPK,
                                   emailaddress = m.emailaddress,
                                   companyname = p.companyname
                               }).ToList();

                ViewState["member"] = members;
                BindMember(Convert.ToInt32(ViewState["Skip"]));
                if (members.Count() > 5)
                {
                    lnkbNext.Visible = true;
                    lnkbPrev.Visible = true;
                    lnkbPrev.Enabled = false;
                }
                else
                {
                    lnkbNext.Visible = false;
                    lnkbPrev.Visible = false;
                }

            }
        }

        private void BindMember(int Skip)
        {
            int iPage = Skip + 10;
            int currPage = iPage / 10;
            double TotalPage;

            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                if (!string.IsNullOrWhiteSpace(txtsearchmember.Text.Trim()))
                {
                    var searchmem = (from m in db_connect.Members
                                     join p in db_connect.Profiles
                                         on m.memberidPK equals p.memberidFK
                                     where m.member_flag.Equals(true)
                                     && m.emailaddress.Contains(txtsearchmember.Text.Trim())
                                     select new MemberList
                                     {
                                         memberidPK = m.memberidPK,
                                         emailaddress = m.emailaddress,
                                         companyname = p.companyname
                                     }).ToList();


                    TotalPage = Math.Ceiling((double)searchmem.Count() / 10);
                    var search_ = searchmem.Skip(Skip).Take(10);
                    if (iPage > searchmem.Count())
                    {
                        lnkbNext.Enabled = false;
                    }
                    else
                    {
                        lnkbNext.Enabled = true;
                    }

                    dlmemberlist.DataSource = search_;
                    dlmemberlist.DataBind();
                    litPageNumber.Text = "Page " + currPage.ToString().Trim() + " of " + (int)TotalPage;


                }
                else
                {
                    var members = (from m in db_connect.Members
                                   join p in db_connect.Profiles
                                      on m.memberidPK equals p.memberidFK
                                   orderby p.lastname
                                   select new MemberList
                                   {
                                       memberidPK = m.memberidPK,
                                       emailaddress = m.emailaddress,
                                       companyname = p.companyname
                                   }).ToList();

                    TotalPage = Math.Ceiling((double)members.Count() / 10);
                    var member_ = members.Skip(Skip).Take(10);
                    if (iPage > members.Count())
                    {
                        lnkbNext.Enabled = false;
                    }
                    else
                    {
                        lnkbNext.Enabled = true;
                    }

                    dlmemberlist.DataSource = member_;
                    dlmemberlist.DataBind();
                    litPageNumber.Text = "Page " + currPage.ToString().Trim() + " of " + (int)TotalPage;

                }


            }

        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //paging + datasource;
            ViewState["Skip"] = 0;
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var searchmem = (from m in db_connect.Members
                                 join p in db_connect.Profiles
                                     on m.memberidPK equals p.memberidFK
                                 where m.member_flag.Equals(true)
                                 && m.emailaddress.Contains(txtsearchmember.Text.Trim())
                                 select new MemberList
                                 {
                                     memberidPK = m.memberidPK,
                                     emailaddress = m.emailaddress,
                                     companyname = p.companyname
                                 }).ToList();

                ViewState["member"] = searchmem;
                BindMember(Convert.ToInt32(ViewState["Skip"]));
                if (searchmem.Count() > 10)
                {
                    lnkbNext.Visible = true;
                    lnkbPrev.Visible = true;
                    lnkbPrev.Enabled = false;
                }
                else
                {
                    lnkbNext.Visible = false;
                    lnkbPrev.Visible = false;
                }

            }
        }

        protected void lnkbNext_Click(object sender, EventArgs e)
        {
            lnkbPrev.Enabled = true;
            ViewState["Skip"] = Convert.ToInt32(ViewState["Skip"]) + 10;
            BindMember(Convert.ToInt32(ViewState["Skip"]));
        }

        protected void lnkbPrev_Click(object sender, EventArgs e)
        {
            ViewState["Skip"] = Convert.ToInt32(ViewState["Skip"]) - 10;
            BindMember(Convert.ToInt32(ViewState["Skip"]));
            if (Convert.ToInt32(ViewState["Skip"]) == 0)
            {
                lnkbPrev.Enabled = false;
            }
        }        
    }
}